<?php

declare(strict_types=1);

namespace AdminBundle\Repositories;

use AdminBundle\Entities\SystemRolesPermissions;
use Belief\Hyperf\Repository;

class SystemRolesPermissionsRepository extends Repository
{
    protected static $entity = SystemRolesPermissions::class;

     /**
     * getPermissionsByRolesIds
     * 获取角色对应的权限集合
     * 对应联查，将权限的分组父级查出来
     * @param int $ids
     * @return array
     */
    public function getPermissionsByRolesIds($ids = 0)
    {
        $table = $this->getModel()->getTable();
        $query = $this->instance()->select($table . '.system_role_id', $table . '.system_permission_id', 'system_permissions.parent_id');
        $query = $query->leftjoin('system_permissions', 'system_permissions.id', '=', $table . '.system_permission_id');
        if (is_array($ids)) {
            $query = $query->whereIn('system_role_id', $ids);
        } else {
            $query = $query->where('system_role_id', $ids);
        }
        $query = $query->get();
        return $query ? $query->toArray() : [];
    }

        /**
     * deleteRolesPermissions
     * 根据roleid删除对应的信息。
     * @param $id
     * @return mixed
     */
    public function deleteRolesPermissions($id)
    {
        $query = $this->query()->where('system_role_id', $id);
        $query = $query->delete();
        return $query;
    }
}
